package com.synesthesiaconsulting.gwt.widgets.debugwidget.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.gwtext.client.widgets.*;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.data.*;
import com.gwtext.client.core.EventObject;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 23, 2008
 * Time: 12:48:42 AM
 * To change this template use File | Settings | File Templates.
 */
public class DebugWidget extends Composite {
    Panel pMain = new Panel();
    GridPanel pGrid = new GridPanel();
    Object[][] log;
    RecordDef mRecDef = new RecordDef (
                new FieldDef[] {
                        new DateFieldDef("time","g:i:s"),
                        new StringFieldDef("module"),
                        new StringFieldDef("message")
                }
        );
    ColumnConfig[] col = new ColumnConfig[] {
                new ColumnConfig("Time","time",50),
                new ColumnConfig("Module","module",65),
                new ColumnConfig("Message","message",300,true,null,"message")
        };

    ColumnModel colMod = new ColumnModel(col);
    public DebugWidget() {
        pMain.setBorder(false);
        pMain.setPaddings(0);
        pMain.setWidth(960);

        log = getStartLog();
        MemoryProxy prox = new MemoryProxy(log);

        ArrayReader reader = new ArrayReader(mRecDef);
        Store store = new Store(prox,reader);
        store.load();
        pGrid.setStore(store);


        pGrid.setColumnModel(colMod);

        pGrid.setFrame(true);
        pGrid.setStripeRows(true);
        pGrid.setWidth(945);
        pGrid.setHeight(250);
        pGrid.setAutoScroll(true);

        


        pGrid.setTitle("Debug Log");
        pMain.add(pGrid);

        initWidget(pMain);
    }

    public void addLogMessage(String mod, String msg) {
        addLog(mod,msg);
        MemoryProxy prox = new MemoryProxy(log);
        ArrayReader reader = new ArrayReader(mRecDef);
        Store store = new Store(prox,reader);
        store.load();
        pGrid.reconfigure(store,colMod);
        pGrid.doLayout();
    }
    private Object[][] getStartLog() {
        Object[][] log = new Object[][]{
                       new Object[]{new Date(),"DebugLog", "DebugLogStarted"}};

        return log;

    }
    private Object[][] addLog(String mod, String msg) {

        int x = log.length;
        Object[][] newLog = new Object[x+1][3];
        for (int i = 0; i < x; i++) {
            newLog[i] = log[i];
        }
        newLog[x] = new Object[]{new Date(),mod,msg};
        log = newLog;
        return log;
    }

   
}
